/*
 * @lc app=leetcode.cn id=147 lang=cpp
 *
 * [147] 对链表进行插入排序
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
#include <bits/stdc++.h>
using namespace std;
struct ListNode;
class Solution
{
public:
    struct MyComp
    {
        bool operator()(ListNode *n1, ListNode *n2)
        {
            return n1->val < n2->val;
        }
    };
    ListNode *insertionSortList(ListNode *head)
    {
        if (!head)
            return head;
        vector<ListNode *> vec;
        while (head)
        {
            vec.push_back(head);
            head = head->next;
        }

        sort(vec.begin(), vec.end(), MyComp());

        head = vec[0];
        auto p = head;
        for(int i = 1; i < vec.size(); i++)
        {
            p->next = vec[i];
            p = vec[i];
        }
        vec[vec.size() - 1]->next = nullptr;
        return head;
    }
};
// @lc code=end
